Imports System.Data.SqlClient
''' <nombre>
''' E-Mail
''' </nombre>
Public Class cls_email_

    Implements ICloneable

#Region "Variables ..."
    Private int_idemail As Integer = 0
    Private str_email As String = ""
    Private int_idtercero As Integer = 0
    Private int_idtipoemail As Integer = 0
    Private bit_primario As Boolean = False

    Private obj_tipoemail As cls_tipoemail_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_email_) = Nothing
#End Region

    Public Overrides Function ToString() As String

        If Not IsNothing(tipoemail) Then
            Select Case str_formatoCombos
                Case "Nombre#########(15) - sigla(5)"
                    Return email.PadRight(15) & " - " & tipoemail.ToString.PadRight(5)
                Case "sigla(5) - Nombre#########(15)"
                    Return tipoemail.ToString.PadRight(5) & " - " & email.PadRight(15)
                Case "Nombre - sigla"
                    Return email & " - " & tipoemail.ToString
                Case "sigla - Nombre"
                    Return tipoemail.ToString & " - " & email
                Case Else
                    Return tipoemail.ToString & " - " & email
            End Select
        Else
            Select Case str_formatoCombos
                Case "Nombre#########(15) - sigla(5)"
                    Return email.PadRight(15)
                Case "sigla(5) - Nombre#########(15)"
                    Return email.PadRight(15)
                Case "Nombre - sigla"
                    Return email
                Case "sigla - Nombre"
                    Return email
                Case Else
                    Return email
            End Select
        End If
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idemail
        End Get
    End Property
    ''' <id>
    ''' true
    ''' </id>
    Property idemail() As Integer
        Get
            idemail = int_idemail
        End Get
        Set(ByVal value As Integer)
            int_idemail = value
        End Set
    End Property
    ''' <nombre>
    ''' E-Mail
    ''' </nombre>
    Property email() As String
        Get
            email = str_email
        End Get
        Set(ByVal value As String)
            str_email = value
        End Set
    End Property
    ''' <nombre>
    ''' Identificador
    ''' </nombre>
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    ''' <nombre>
    ''' Identificador
    ''' </nombre>
    Property idtipoemail() As Integer
        Get
            idtipoemail = int_idtipoemail
        End Get
        Set(ByVal value As Integer)
            int_idtipoemail = value
        End Set
    End Property
    Property primario() As Boolean
        Get
            primario = bit_primario
        End Get
        Set(ByVal value As Boolean)
            bit_primario = value
        End Set
    End Property
    ''' <importar>
    ''' True
    ''' </importar>
    Property tipoemail() As cls_tipoemail_
        Get
            If IsNothing(obj_tipoemail) Then
                obj_tipoemail = New cls_tipoemail_
                If Not obj_tipoemail.Buscar(idtipoemail) Then
                    obj_tipoemail = Nothing
                End If
            End If
            tipoemail = obj_tipoemail
        End Get
        Set(ByVal value As cls_tipoemail_)
            obj_tipoemail = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_email_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_email_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_email_)
        While obj_Reader.Read()
            Dim Elemento As New cls_email_

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idemail"))) = False Then
                Elemento.int_idemail = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idemail")))
            End If

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("email"))) = False Then
                Elemento.str_email = (obj_Reader.GetValue(obj_Reader.GetOrdinal("email")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtipoemail"))) = False Then
                Elemento.int_idtipoemail = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtipoemail")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
                Elemento.bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Public Sub CargarVariables(ByVal obj_Reader As SqlDataReader, Optional read As Boolean = True)
        Dim tabla As String = ""
        If read Then
            obj_Reader.Read()
        Else
            tabla = "email_"
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idemail"))) = False Then
            int_idemail = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idemail")))
        End If

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "email"))) = False Then
            str_email = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "email")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtercero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtipoemail"))) = False Then
            int_idtipoemail = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "idtipoemail")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "primario"))) = False Then
            bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal(tabla & "primario")))
        End If

        If Not read Then
            tipoemail = New cls_tipoemail_
            tipoemail.CargarVariables(obj_Reader, False)
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__Nuevo"
                .Parameters.Add("@email", SqlDbType.NVarChar).Value = str_email
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idtipoemail", SqlDbType.Int).Value = IIf(int_idtipoemail = 0, DBNull.Value, int_idtipoemail)
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
            Nuevo = True
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__Modificar"
                .Parameters.Add("@idemail", SqlDbType.Int).Value = int_idemail
                .Parameters.Add("@email", SqlDbType.NVarChar).Value = str_email
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@idtipoemail", SqlDbType.Int).Value = IIf(int_idtipoemail = 0, DBNull.Value, int_idtipoemail)
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idemail As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__Eliminar"
                .Parameters.Add("@idemail", SqlDbType.Int).Value = idemail
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idemail As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__Buscar"
                .Parameters.Add("@idemail", SqlDbType.Int).Value = idemail
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidtercero(ByVal idtercero As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidtercero = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__CargarXidtercero"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = idtercero
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidtercero = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidtercero los registros de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidterceroprimario(ByVal idtercero As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidterceroprimario = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "email__CargarXidterceroprimario"
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = idtercero
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                CargarXidterceroprimario = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarXidterceroprimario los registros de email_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_email_Clon As New cls_email_
        obj_email_Clon.int_idemail = Me.int_idemail
        obj_email_Clon.str_email = Me.str_email
        obj_email_Clon.int_idtercero = Me.int_idtercero
        obj_email_Clon.int_idtipoemail = Me.int_idtipoemail
        obj_email_Clon.bit_primario = Me.bit_primario
        obj_email_Clon.col_Items = Me.col_Items
        Return obj_email_Clon
    End Function

End Class
